package com.tjetc.adminmanage;

import com.tjetc.domian.Communityfee;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.FeeService;
import com.tjetc.vo.Result;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

//费用管理
public class FeeManageView {
    private static Scanner sc = new Scanner(System.in);
    private FeeService feeService = ClientProxy.getProxyInstance(FeeService.class);
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    public void start() {
        while (true) {
            System.out.println("费用管理菜单");
            System.out.println("1.查询费用信息");
            System.out.println("2.添加缴费信息");
            System.out.println("3.删除缴费信息");
            System.out.println("4.修改缴费信息");
            System.out.println("5.查询费用信息(按缴费日期)");
            System.out.println("0.返回上一级");
            switch (sc.nextInt()) {
                case 1:
                    list();
                    break;
                case 2:
                    add();
                    break;
                case 3:
                    del();
                    break;
                case 4:
                    updata();
                    break;
                case 5:
                    listByDate();
                    break;
                case 0:
                    return;
                default:
                    System.out.println("输入有误，请重新输入");
            }
        }

    }

    private void listByDate() {
        System.out.println("查询费用信息(按缴费日期)");
        List<Communityfee> list = feeService.selectAllByDate();
        if (list == null) {
            System.out.println("没有数据");
        } else {
            for (Communityfee communityfee : list) {
                System.out.println(communityfee);
            }
        }

    }

    private void updata() {
        System.out.println("修改费用信息");
        System.out.println("输入要修改的费用id");
        int feeid = sc.nextInt();
        System.out.println("请输入费用所在公寓");
        int apartmentId = sc.nextInt();
        System.out.println("请输入费用类型");
        String feeType = sc.next();
        System.out.println("请输入费用金额");
        BigDecimal money = sc.nextBigDecimal();
        System.out.println("请输入缴费日期 格式:yyyy-MM-dd");
        String date = sc.next();
        System.out.println("请输入截止日期 格式:yyyy-MM-dd");
        String paymentdate = sc.next();
        try {
            Date date1 = dateFormat.parse(date);
            Date date2 = dateFormat.parse(paymentdate);
            Communityfee communityfee = new Communityfee(feeid, apartmentId, feeType, money, date1, date2);
            Result result = feeService.updata(communityfee);
            System.out.println(result.getMsg());
        } catch (ParseException e) {

        }


    }

    private void del() {
        System.out.println("删除费用信息");
        System.out.println("输入费用id");
        int feeid = sc.nextInt();
        Result result = feeService.del(feeid);
        System.out.println(result.getMsg());

    }

    private void add() {
        System.out.println("请输入费用所在公寓");
        int apartmentId = sc.nextInt();
        System.out.println("请输入费用类型");
        String feeType = sc.next();
        System.out.println("请输入费用金额");
        BigDecimal money = sc.nextBigDecimal();
        System.out.println("请输入缴费日期 格式:yyyy-MM-dd");
        String date = sc.next();
        System.out.println("请输入截止日期 格式:yyyy-MM-dd");
        String paymentdate = sc.next();
        try {
            Date date1 = dateFormat.parse(date);
            Date date2 = dateFormat.parse(paymentdate);
            Communityfee communityfee = new Communityfee();
            communityfee.setApartmentid(apartmentId);
            communityfee.setFeetype(feeType);
            communityfee.setAmount(money);
            communityfee.setDuedate(date1);
            communityfee.setPaymentdate(date2);
            Result result = feeService.add(communityfee);
            System.out.println(result.getMsg());
        } catch (ParseException e) {
            System.out.println("输入的日期格式错误，请按照提示格式输入！");
        }


    }

    private void list() {
        System.out.println("费用信息如下：");
        List<Communityfee> list = feeService.selectAll();
        if (list == null) {
            System.out.println("没有数据");
        } else {
            for (Communityfee communityfee : list) {
                System.out.println(communityfee);
            }
        }

    }
}
